Ext.define('cloud.controller.App', {
    extend: 'Ext.app.Controller',
    config: {
        refs: {
            appview: 'appview',
            appshow: 'appshow',
            appadd: 'appadd',
            zhifubao: 'zhifubao',
            appfunction: 'appfunction',
            appfunctionadd: 'appfunctionadd',
            search_app: 'appview #search_app',
            select_sort: 'appview #select_sort',
            btn_add: 'appview #btn_add',
            addSubmit: 'appview #submit',
            submit_edit: 'appshow #submit_edit',
            submit_del: 'appshow #submit_del',
            zhifubao_save: 'zhifubao #zhifubao_save',
            function_add: 'appfunction #function_add',
            funcion_app_add: 'appfunctionadd #funcion_app_add',
            function_id: 'appfunctionadd checkboxfield[name=function_id]',
            functionList: 'appfunction #functionList'
        },
        control: {
            /**
             * search app
             */
            search_app: {
                change: function () {
                    var appStore = Ext.getStore('AppAllFindStore');
                    var value = this.getSearch_app().getValue();
                    appStore.clearFilter();
                    if (value != "") {
                        appStore.filter('name', value, true);
                    }
                    appStore.load();
                }
            },
            /**
             * sort app
             */
            select_sort: {
                change: function (item, newValue, oldValue) {
                    var appAllStore = Ext.getStore('AppAllFindStore');
                    var sortStr = newValue.split("_");
                    appAllStore.sort({
                        property: sortStr[0],
                        direction: sortStr[1]
                    });
                    appAllStore.loadPage(1);
                }
            },
            /**
             * show appadd page
             */
            btn_add: {
                tap: function () {
                    this.getAppview().push(this.getAppadd() || Ext.create('cloud.view.app.AppAdd'));
                }
            },
            /**
             * show appshow(or edit) page
             */
            funcion_app_add: {
                tap: function () {
                    var caid = Ext.getCmp('app_id').getValue();
                    var appfunctionfids = this.getFunction_id().getGroupValues();
                    if (Ext.isEmpty(appfunctionfids)) {
                        Ext.Msg.alert('没有选择任何功能');
                        return;
                    }
                    Ext.Ajax.request({
                        url: GLOBAL.api_url + 'app_function_add',
                        params: {
                            caid: caid,
                            fids: appfunctionfids.join(',')
                        },
                        scope: this,
                        success: function (response) {
                            Ext.Viewport.unmask();
                            var result = Ext.decode(response.responseText);
                            Ext.Msg.alert(result.Variables.Message.messagestr);
                            var appfunctionstore = Ext.getStore('AppFunction').load({params: {caid: caid}});
                            this.getAppview().pop()
                        },
                        failure: function () {
                            Ext.Viewport.unmask();
                            Ext.Msg.alert('服务器维护中...')
                            return false;
                        }
                    })
                }
            },
            function_add: {
                tap: function () {
                    var appfunctionadd = this.getAppfunctionadd() || Ext.create('cloud.view.app.AppFunctionnAdd');
                    Ext.Viewport.mask({xtype: 'loadmask' });
                    //未选择的功能
                    Ext.Ajax.request({
                        url: GLOBAL.api_url + 'app_function_not_select',
                        params: {caid: Ext.getCmp('app_id').getValue()},
                        scope: this,
                        success: function (response) {
                            Ext.Viewport.unmask();
                            var result = Ext.decode(response.responseText);
                            var checkboxs = [];
                            var data = result.Variables.data;
                            for (var i = 0; i < data.length; i++) {
                                checkboxs.push(
                                    {
                                        layout: 'hbox',
                                        items: [
                                            {
                                                style: '-webkit-box-orient: horizontal;',
                                                xtype: 'checkboxfield',
                                                name: 'function_id',
                                                label: data[i].name,
                                                value: data[i].id,
                                                labelWidth: '80%',
                                                width: '100%'
                                            }
                                        ]
                                    }
                                );
                            }
                            Ext.getCmp('appfunctioncheck').setItems(checkboxs);
                        },
                        failure: function () {
                            Ext.Viewport.unmask();
                            Ext.Msg.alert('服务器维护中...')
                            return false;
                        }
                    })
                    this.getAppview().push(appfunctionadd);
                }
            },
            'appview list': {
                itemtap: function (list, item, index, record, target, event) {
                    Ext.Viewport.mask({xtype: 'loadmask' });
                    if (list.id == 'selectID') {
                        var appview = this.getAppview();
                        //设置支付宝
                        if (target.target.name == 'zhifubao') {
                            var zhifubao = this.getZhifubao() || Ext.create('cloud.view.app.Zhifubao');
                            Ext.getCmp('zhifubao_caid').setValue(record.get('caid'))
                            Ext.Ajax.request({
                                url: GLOBAL.api_url + 'app_alipay_find',
                                params: {caid: record.get('caid')},
                                scope: this,
                                success: function (response) {
                                    Ext.Viewport.unmask();
                                    var result = Ext.decode(response.responseText);
                                    Ext.getCmp('zhifubao_partner').setValue(result.Variables.data.partner);
                                    Ext.getCmp('zhifubao_key').setValue(result.Variables.data.key);
                                    Ext.getCmp('zhifubao_seller_email').setValue(result.Variables.data.seller_email);
                                    appview.push(zhifubao);
                                },
                                failure: function () {
                                    Ext.Viewport.unmask();
                                    Ext.Msg.alert('服务器维护中...')
                                    return false;
                                }
                            })
                        } else if (target.target.name == 'appfunction') {
                            //进入应用功能列表
                            var appfunction = this.getAppfunction() || Ext.create('cloud.view.app.AppFunction');
                            var appfunctionstore = Ext.getStore('AppFunction');
                            appfunctionstore.load({params: {caid: record.get('caid')}});
                            Ext.getCmp('app_id').setValue(record.get('caid'));
                            appview.push(appfunction);
                        }
                        else {
                            //应用详情页
                            var appinfostore = Ext.getStore('AppInfoGetStore');
                            var appshow = this.getAppshow() || Ext.create('cloud.view.app.AppShow');
                            appshow.setRecord(record);
                            appview.push(appshow);
                        }
                    } else {
                        //点击删除应用功能
                        if (target.target.name == 'delet_function') {
                            Ext.Msg.confirm('确认删除？', '', function (data) {
                                if (data == 'yes') {
                                    Ext.Viewport.mask({xtype: 'loadmask' });
                                    Ext.Ajax.request({
                                        url: GLOBAL.api_url + 'app_function_delete',
                                        params: {
                                            id: record.get('id')
                                        },
                                        scope: this,
                                        success: function (response) {
                                            var result = Ext.decode(response.responseText);
                                            Ext.Msg.alert(result.Variables.Message.messagestr);
                                            Ext.getStore('AppFunction').load({params: {caid: Ext.getCmp('app_id').getValue()}});
                                        },
                                        failure: function () {
                                            Ext.Msg.alert('服务器维护中...')
                                            return false;
                                        }
                                    })
                                }
                                Ext.Viewport.unmask();
                            }, this);
                        }

                    }

                    Ext.Viewport.unmask();
                }
            },

            appfunction: {
                itemtap: function (list, item, index, record, target, event) {
                    console.log('delet')
                }
            },

            /**
             * insert app
             */
            addSubmit: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    var name = Ext.getCmp('name').getValue(),
                        enname = Ext.getCmp('enname').getValue(),
                        version = Ext.getCmp('version').getValue(),
                        integraltype = Ext.getCmp('integraltype').getValue(),
                        discription = Ext.getCmp('discription').getValue();
                    var app = Ext.create('cloud.model.AppModel', {name: name, enname: enname, version: version, integraltype: integraltype, discription: discription});
                    var errors = app.validate();
                    Ext.Viewport.unmask();

                    this.getAppadd().submit({
                        url: GLOBAL.api_url + 'app_add',
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                }
            },
            /**
             * update app
             */
            submit_edit: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getAppshow().submit({
                        url: GLOBAL.api_url + 'app_update',
                        scope: this,
                        method: 'POST',
                        success: this.executeSuccess
                    });
                    Ext.Viewport.unmask();
                }
            },
            /**
             * delete app
             */
            submit_del: {
                tap: function () {
                    Ext.Msg.confirm('是否删除', '', function (data) {
                        if (data == 'yes') {
                            this.getAppshow().submit({
                                url: GLOBAL.api_url + 'app_update&disabled=true',
                                scope: this,
                                method: 'POST',
                                success: this.executeSuccess
                            });
                        }
                    }, this);
                }
            },
            /**
             * zhifubao save
             */
            zhifubao_save: {
                tap: function () {
                    Ext.Viewport.mask({xtype: 'loadmask'});
                    this.getZhifubao().submit({
                        url: GLOBAL.api_url + 'app_alipay_update',
                        scope: this,
                        method: 'POST',
                        success: function (from, result) {
                            Ext.Msg.alert(result.Variables.Message.messagestr);
                            if (result.Variables.Result.code == 0) {
                                this.getAppview().pop();
                            }
                        }
                    });
                    Ext.Viewport.unmask();
                }
            }
        },
        pagePicker: undefined,
        id: undefined
    },
    executeSuccess: function (from, result) {
        if (result.Variables.Result.code == 0) {
            Ext.getStore('AppAllFindStore').load();
            this.getAppview().pop();
        } else {
            Ext.Msg.alert(result.Variables.Message.messagestr);
        }
    }
});